package winnu.gui;
import winnu.control.ReplaceItemController;
import winnu.control.WinnuControl;
import javax.swing.DefaultListModel;
import java.util.List;
import org.apache.torque.TorqueException;

import winnu.dao.Item;
import winnu.dao.ItemBatch;
import winnu.dao.ItemPeer;
import winnu.dao.Sale;
import winnu.dao.WithdrawnItem;
import winnu.dao.WithdrawnItemPeer;
import winnu.test.InitTorque;

import java.util.ArrayList;
import java.util.List;

public class ReplaceItemPanel extends javax.swing.JPanel {
	private WinnuControl control;
	private List<Item> lstItemBatch;
	private DefaultListModel lstModel = new DefaultListModel();

    /** Creates new form PanelReplaceItem */
    public ReplaceItemPanel() {
        initComponents();
    }

	public void setControl(WinnuControl control){
        this.control = control;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

    	withdrawnItems = new java.util.ArrayList<WithdrawnItem>();
        jScrollPane2 = new javax.swing.JScrollPane();
        rbtnGroup = new javax.swing.ButtonGroup();
        labelReplaceItem = new javax.swing.JLabel();
        labelItemName = new javax.swing.JLabel();
        labelItemQuantity = new javax.swing.JLabel();
        txtQuantity = new javax.swing.JTextField();
        labelReason = new javax.swing.JLabel();
        labelItemName1 = new javax.swing.JLabel();
        lblBrandName = new javax.swing.JLabel();
        lblGenericName = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        lstResults = new javax.swing.JList();
        btnConfirm = new javax.swing.JButton();
        btnSearchItem = new javax.swing.JButton();
        labelItemQuantity1 = new javax.swing.JLabel();
        rbtnDamaged = new javax.swing.JRadioButton();
        rbtnExpired = new javax.swing.JRadioButton();


        labelReplaceItem.setFont(new java.awt.Font("Tahoma", 0, 18));
        labelReplaceItem.setText("Replacement of Item");

        labelItemName.setText("Brand Name: ");

        labelItemQuantity.setText("Quantity:");

        txtQuantity.setMaximumSize(null);
        txtQuantity.setMinimumSize(null);

        labelReason.setText("Reason:");

        labelItemName1.setText("Generic Name:");

        lblBrandName.setText("<Select an Item>");

        lblGenericName.setText("<Select an Item>");

        lstResults.setMaximumSize(null);
        lstResults.setMinimumSize(null);
        jScrollPane3.setViewportView(lstResults);

        btnConfirm.setText("Confirm");
        btnConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmActionPerformed(evt);
            }
        });

        btnSearchItem.setText("Select Item");
        btnSearchItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchItemActionPerformed(evt);
            }
        });

        labelItemQuantity1.setText("Choose a batch:");

        rbtnDamaged.setText("Damaged medicine/s");

        rbtnExpired.setText("Expired medicine/s");

        rbtnGroup.add(rbtnDamaged);
        rbtnGroup.add(rbtnExpired);
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(labelReplaceItem))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(22, 22, 22)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(labelItemName)
                                    .addComponent(labelItemName1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblGenericName)
                                    .addComponent(lblBrandName)))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(13, 13, 13)
                                .addComponent(labelItemQuantity)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(rbtnDamaged)
                                    .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(rbtnExpired)
                                    .addComponent(btnConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(91, 91, 91)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnSearchItem, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelItemQuantity1)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addComponent(labelReason)))
                .addContainerGap(927, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelReplaceItem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(labelItemName1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblBrandName)
                            .addComponent(labelItemName))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblGenericName)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSearchItem)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(labelItemQuantity1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelItemQuantity)
                    .addComponent(txtQuantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelReason)
                    .addComponent(rbtnDamaged))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbtnExpired)
                .addGap(18, 18, 18)
                .addComponent(btnConfirm)
                .addContainerGap(164, Short.MAX_VALUE))
        );
    }// </editor-fold>

    private void btnConfirmActionPerformed(java.awt.event.ActionEvent evt) {
    	if(!lstResults.getSelectedValue().equals(null)) {
    		String reason = rbtnDamaged.isSelected() ? rbtnDamaged.getText() : rbtnExpired.getText();
        	int qReplaced = Integer.parseInt(txtQuantity.getText());
        	int itemBatchId = WithdrawnItemPeer.retrieveWithdrawnItem(lstResults.getSelectedIndex() + 1).getItemBatchId();
        	String dateWithdrawn = WithdrawnItemPeer.retrieveWithdrawnItem(lstResults.getSelectedIndex() + 1).getDateWithdrawn();
        	String customer = null;
        	
        	try {
        		customer = WithdrawnItemPeer.retrieveWithdrawnItem(lstResults.getSelectedIndex() + 1).getSale().getCustomerName();
        	} catch(TorqueException e) { 
        		e.printStackTrace();
        	}
    		
    		System.out.println(itemBatchId);
			System.out.println(dateWithdrawn);
			System.out.println(qReplaced);
			System.out.println(customer);
			System.out.println(reason);
    	}
    }

    private void btnSearchItemActionPerformed(java.awt.event.ActionEvent evt) {
    	SearchItemFrame searchItemFrame = new SearchItemFrame(this.control);
		searchItemFrame.setVisible(true);

        control.setCurrentSelectedItem(null);
        
        searchItemFrame.addWindowListener(new java.awt.event.WindowAdapter(){
        	public void windowClosed(java.awt.event.WindowEvent e){
        		updateView();
        	}        	
        });
    }
    
    private void updateView(){
    	lstItemBatch = control.itemSearchController.searchItem(control.getCurrentSelectedItem().getBrandName());
    	DefaultListModel items;
		//withdrawnItems = WithdrawnItemPeer.retrieveAllItemBatchId(control.getCurrentSelectedItem().getBrandName());
		/*
		for(int i = 0; i < withdrawnItems.size(); i++) {
			System.out.println("ITEM:" + withdrawnItems.get(i).getItemBatchId().toString());
		}*/
		
    	if(!control.getCurrentSelectedItem().equals(null)){
			lblGenericName.setText(control.getCurrentSelectedItem().getGenericName());
			lblBrandName.setText(control.getCurrentSelectedItem().getBrandName());
			btnSearchItem.setText("Change Item");

			items = control.replaceItemController.getWithdrawnItems();
			
			/* START
			//List<WithdrawnItem> withdrawnItems = new ArrayList<WithdrawnItem>();
			WithdrawnItem ci;
			Sale sale;
			Item item;
			ItemBatch itemBatch;
					
	    	int itemId;
	    	int itemBatchId;
	    	int saleId;
			
			withdrawnItems = WithdrawnItemPeer.retrieveAllByItemBrandName(control.getCurrentSelectedItem().getBrandName());
			
			lstModel.clear();
			
			for(int i=0;i<withdrawnItems.size();i++){
				ci = withdrawnItems.get(i);

				try {
					if((sale=ci.getSale())!=null){
						lstModel.add(i, "SaleID: " + sale.getSaleId() + " -- Batch: " + ci.getItemBatchId() +" -- Date: " + sale.getSaleDate() + " -- Quantity: " + ci.getQuantity() + " -- Customer: " + sale.getCustomerName());
					}
		
				} catch (TorqueException e) {
					e.printStackTrace();
				}
			}
			
			lstResults.setModel(lstModel);
			/* END */
	    	lstResults.setModel(items);
    	}
    	
	}

    // Variables declaration - do not modify
    private java.util.List<WithdrawnItem> withdrawnItems;
    private javax.swing.JButton btnConfirm;
    private javax.swing.JButton btnSearchItem;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel labelItemName;
    private javax.swing.JLabel labelItemName1;
    private javax.swing.JLabel labelItemQuantity;
    private javax.swing.JLabel labelItemQuantity1;
    private javax.swing.JLabel labelReason;
    private javax.swing.JLabel labelReplaceItem;
    private javax.swing.JLabel lblBrandName;
    private javax.swing.JLabel lblGenericName;
    private javax.swing.JList lstResults;
    private javax.swing.ButtonGroup rbtnGroup;
    private javax.swing.JRadioButton rbtnDamaged;
    private javax.swing.JRadioButton rbtnExpired;
    private javax.swing.JTextField txtQuantity;
    // End of variables declaration
}
